/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.entitymanager;

import database.entitymanager.exceptions.NonexistentEntityException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import database.entity.ItemGroup;
import database.entity.ShopItem;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Stefan
 */
public class ItemGroupManager {

    
    private static EntityManagerFactory emf = EntityFactoryHolder.getInstance();

    public static EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public static void create(ItemGroup itemGroup) {
        if (itemGroup.getShopItemList() == null) {
            itemGroup.setShopItemList(new ArrayList<ShopItem>());
        }
        if (itemGroup.getItemGroupList() == null) {
            itemGroup.setItemGroupList(new ArrayList<ItemGroup>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ItemGroup itemGroupRel = itemGroup.getItemGroup();
            if (itemGroupRel != null) {
                itemGroupRel = em.getReference(itemGroup.getClass(), itemGroup.getItemgroupid());
                itemGroup.setItemGroup(itemGroupRel);
            }
            List<ShopItem> attachedShopItemList = new ArrayList<ShopItem>();
            for (ShopItem shopItemListShopItemToAttach : itemGroup.getShopItemList()) {
                shopItemListShopItemToAttach = em.getReference(shopItemListShopItemToAttach.getClass(), shopItemListShopItemToAttach.getItemid());
                attachedShopItemList.add(shopItemListShopItemToAttach);
            }
            itemGroup.setShopItemList(attachedShopItemList);
            List<ItemGroup> attachedItemGroupList = new ArrayList<ItemGroup>();
            for (ItemGroup itemGroupListItemGroupToAttach : itemGroup.getItemGroupList()) {
                itemGroupListItemGroupToAttach = em.getReference(itemGroupListItemGroupToAttach.getClass(), itemGroupListItemGroupToAttach.getItemgroupid());
                attachedItemGroupList.add(itemGroupListItemGroupToAttach);
            }
            itemGroup.setItemGroupList(attachedItemGroupList);
            em.persist(itemGroup);
            if (itemGroupRel != null) {
                itemGroupRel.getItemGroupList().add(itemGroup);
                itemGroupRel = em.merge(itemGroupRel);
            }
            for (ShopItem shopItemListShopItem : itemGroup.getShopItemList()) {
                shopItemListShopItem.getItemGroupList().add(itemGroup);
                shopItemListShopItem = em.merge(shopItemListShopItem);
            }
            for (ItemGroup itemGroupListItemGroup : itemGroup.getItemGroupList()) {
                ItemGroup oldItemGroupOfItemGroupListItemGroup = itemGroupListItemGroup.getItemGroup();
                itemGroupListItemGroup.setItemGroup(itemGroup);
                itemGroupListItemGroup = em.merge(itemGroupListItemGroup);
                if (oldItemGroupOfItemGroupListItemGroup != null) {
                    oldItemGroupOfItemGroupListItemGroup.getItemGroupList().remove(itemGroupListItemGroup);
                    oldItemGroupOfItemGroupListItemGroup = em.merge(oldItemGroupOfItemGroupListItemGroup);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static void edit(ItemGroup itemGroup) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ItemGroup persistentItemGroup = em.find(ItemGroup.class, itemGroup.getItemgroupid());
            ItemGroup itemGroupRelOld = persistentItemGroup.getItemGroup();
            ItemGroup itemGroupRelNew = itemGroup.getItemGroup();
            List<ShopItem> shopItemListOld = persistentItemGroup.getShopItemList();
            List<ShopItem> shopItemListNew = itemGroup.getShopItemList();
            List<ItemGroup> itemGroupListOld = persistentItemGroup.getItemGroupList();
            List<ItemGroup> itemGroupListNew = itemGroup.getItemGroupList();
            if (itemGroupRelNew != null) {
                itemGroupRelNew = em.getReference(itemGroupRelNew.getClass(), itemGroupRelNew.getItemgroupid());
                itemGroup.setItemGroup(itemGroupRelNew);
            }
            List<ShopItem> attachedShopItemListNew = new ArrayList<ShopItem>();
            for (ShopItem shopItemListNewShopItemToAttach : shopItemListNew) {
                shopItemListNewShopItemToAttach = em.getReference(shopItemListNewShopItemToAttach.getClass(), shopItemListNewShopItemToAttach.getItemid());
                attachedShopItemListNew.add(shopItemListNewShopItemToAttach);
            }
            shopItemListNew = attachedShopItemListNew;
            itemGroup.setShopItemList(shopItemListNew);
            List<ItemGroup> attachedItemGroupListNew = new ArrayList<ItemGroup>();
            for (ItemGroup itemGroupListNewItemGroupToAttach : itemGroupListNew) {
                itemGroupListNewItemGroupToAttach = em.getReference(itemGroupListNewItemGroupToAttach.getClass(), itemGroupListNewItemGroupToAttach.getItemgroupid());
                attachedItemGroupListNew.add(itemGroupListNewItemGroupToAttach);
            }
            itemGroupListNew = attachedItemGroupListNew;
            itemGroup.setItemGroupList(itemGroupListNew);
            itemGroup = em.merge(itemGroup);
            if (itemGroupRelOld != null && !itemGroupRelOld.equals(itemGroupRelNew)) {
                itemGroupRelOld.getItemGroupList().remove(itemGroup);
                itemGroupRelOld = em.merge(itemGroupRelOld);
            }
            if (itemGroupRelNew != null && !itemGroupRelNew.equals(itemGroupRelOld)) {
                itemGroupRelNew.getItemGroupList().add(itemGroup);
                itemGroupRelNew = em.merge(itemGroupRelNew);
            }
            for (ShopItem shopItemListOldShopItem : shopItemListOld) {
                if (!shopItemListNew.contains(shopItemListOldShopItem)) {
                    shopItemListOldShopItem.getItemGroupList().remove(itemGroup);
                    shopItemListOldShopItem = em.merge(shopItemListOldShopItem);
                }
            }
            for (ShopItem shopItemListNewShopItem : shopItemListNew) {
                if (!shopItemListOld.contains(shopItemListNewShopItem)) {
                    shopItemListNewShopItem.getItemGroupList().add(itemGroup);
                    shopItemListNewShopItem = em.merge(shopItemListNewShopItem);
                }
            }
            for (ItemGroup itemGroupListOldItemGroup : itemGroupListOld) {
                if (!itemGroupListNew.contains(itemGroupListOldItemGroup)) {
                    itemGroupListOldItemGroup.setItemGroup(null);
                    itemGroupListOldItemGroup = em.merge(itemGroupListOldItemGroup);
                }
            }
            for (ItemGroup itemGroupListNewItemGroup : itemGroupListNew) {
                if (!itemGroupListOld.contains(itemGroupListNewItemGroup)) {
                    ItemGroup oldItemGroupOfItemGroupListNewItemGroup = itemGroupListNewItemGroup.getItemGroup();
                    itemGroupListNewItemGroup.setItemGroup(itemGroup);
                    itemGroupListNewItemGroup = em.merge(itemGroupListNewItemGroup);
                    if (oldItemGroupOfItemGroupListNewItemGroup != null && !oldItemGroupOfItemGroupListNewItemGroup.equals(itemGroup)) {
                        oldItemGroupOfItemGroupListNewItemGroup.getItemGroupList().remove(itemGroupListNewItemGroup);
                        oldItemGroupOfItemGroupListNewItemGroup = em.merge(oldItemGroupOfItemGroupListNewItemGroup);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = itemGroup.getItemgroupid();
                if (findItemGroup(id) == null) {
                    throw new NonexistentEntityException("The itemGroup with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ItemGroup itemGroup;
            try {
                itemGroup = em.getReference(ItemGroup.class, id);
                itemGroup.getItemgroupid();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The itemGroup with id " + id + " no longer exists.", enfe);
            }
            ItemGroup itemGroupRel = itemGroup.getItemGroup();
            if (itemGroupRel != null) {
                itemGroupRel.getItemGroupList().remove(itemGroup);
                itemGroupRel = em.merge(itemGroupRel);
            }
            List<ShopItem> shopItemList = itemGroup.getShopItemList();
            for (ShopItem shopItemListShopItem : shopItemList) {
                shopItemListShopItem.getItemGroupList().remove(itemGroup);
                shopItemListShopItem = em.merge(shopItemListShopItem);
            }
            List<ItemGroup> itemGroupList = itemGroup.getItemGroupList();
            for (ItemGroup itemGroupListItemGroup : itemGroupList) {
                itemGroupListItemGroup.setItemGroup(null);
                itemGroupListItemGroup = em.merge(itemGroupListItemGroup);
            }
            em.remove(itemGroup);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static List<ItemGroup> findItemGroupEntities() {
        return findItemGroupEntities(true, -1, -1);
    }

    public static List<ItemGroup> findItemGroupEntities(int maxResults, int firstResult) {
        return findItemGroupEntities(false, maxResults, firstResult);
    }

    private static List<ItemGroup> findItemGroupEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ItemGroup.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public static ItemGroup findItemGroup(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ItemGroup.class, id);
        } finally {
            em.close();
        }
    }

    public static int getItemGroupCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ItemGroup> rt = cq.from(ItemGroup.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
